package com.sw.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sw.model.AddressManage;
import com.sw.service.AddressManageService;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller
@RequestMapping("/address")
public class AddressManageController {

	@Autowired
	private AddressManageService addressManageService;
	
	/**
	 * 添加
	 * @param address
	 * @return
	 */
	@RequestMapping(value="/add",method = RequestMethod.POST)
	public @ResponseBody String addAddress(AddressManage address){
		boolean result = addressManageService.insertAddress(address);
		JSONObject jsonObject = new JSONObject();  
		if(result){
		    jsonObject.put("msg", "success");  
		}else{
			jsonObject.put("msg", "failure");  
		}
		 
	    return jsonObject.toString();
	}
	
	/**
	 * 修改
	 * @param address
	 * @return
	 */
	@RequestMapping(value="/update",method = RequestMethod.POST)
	public @ResponseBody Object updateAddress(@RequestBody AddressManage address){
		boolean result = addressManageService.updateAddress(address);
		JSONObject jsonObject = new JSONObject();  
		if(result){
		    jsonObject.put("msg", "success");  
		}else{
			jsonObject.put("msg", "failure");  
		}
		 
	    return jsonObject.toString();
	}
	
	/**
	 * 根据id删除
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/delete/{id}",method = RequestMethod.GET)
	public @ResponseBody Object deleteModule(@PathVariable("id") int id){
		boolean result = addressManageService.deleteAddressById(id);
		JSONObject jsonObject = new JSONObject();  
		if(result){
			jsonObject.put("msg", "success");  
		}else{
			jsonObject.put("msg", "failure");  
		}
        
        return jsonObject;  
	}
	
	/**
	 * 查询全部
	 * @return
	 */
	@RequestMapping(value="/selectAll",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
	public @ResponseBody String selectModuleAll(){
		List<AddressManage> list = addressManageService.getAddressAll();
		JSONObject jsonObject;
		JSONArray jsonArray = new JSONArray();
		for (AddressManage address : list) {
			jsonObject = JSONObject.fromObject(address);
			jsonArray.add(jsonObject);
		}
		
		return jsonArray.toString();
	}
	
	/**
	 * 根据ID查询
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/selectById/{id}",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
	public @ResponseBody String selectModule(@PathVariable("id") int id){
		System.out.println(id);
		AddressManage addressManage = addressManageService.getById(id);
		JSONObject jsonObject = JSONObject.fromObject(addressManage);
		return jsonObject.toString();
	}
}
